<template>
	<view class="page">
		<view class="loading_css" v-if="isShow">
			<u-loading size="36" color="#FC5B03"></u-loading>
			<view>加载中</view>
		</view>
		<template v-else>
			<view class="img_view">
				<image :src='STATIC_URL+"15.png"' mode="aspectFill"></image>
				<view class="car_tip flex align-center justify-center">年款/颜色等以门店为准</view>
			</view>
			<view class="select_vehicle">
				<view class="car_name flex align-center">
					<view class="car_name_1">{{selectCarInfo.name}}</view>
					<image v-if="selectCarInfo.type == 2" class="car_icon" :src='STATIC_URL+"16.png"'
						mode="aspectFill"></image>
					<view class="car_name_2">
						<text>{{selectCarInfo.seats}}座</text>
						<text class="car_name_2_1">丨</text>
						<text>{{selectCarInfo.gearbox}}</text>
						<text class="car_name_2_1">丨</text>
						<text>{{selectCarInfo.power}}</text>
						<text class="car_name_2_1">丨</text>
						<text>{{selectCarInfo.model}}</text>
					</view>
				</view>
				<view class="vehicle_label">
					<view class="label_le">
						<template v-if="selectCarInfo.tags">
							<template v-for="(v, k) in (selectCarInfo.tags).split(',')">
								<!-- #ifdef MP-ALIPAY -->
								<view v-if="v == 1" class="flex align-center justify-center label_3">
									<image :src='STATIC_URL+"9.png"'></image>
									<text>芝麻免押</text>
								</view>
								<!-- #endif -->
								<!-- #ifndef MP-ALIPAY -->
								<image v-if="v == 1" class="label_1" :src='STATIC_URL+"219.png"' mode="aspectFill">
								</image>
								<!-- #endif -->
								<view class="label_2" v-if="v != 1">{{getTag(v)}}</view>
							</template>
						</template>
					</view>
				</view>
			</view>
			<view class="quotation_list">
				<view class="quotation_scroll">
					<view class="quotation_item" v-for="(item, index) in offerList" :key="index"
						:class="item.customer_id == 1 ? 'quotation_bg' : ''" @tap.stop="goReserve(item)">
						<view class="quotation_information">
							<view class="information">
								<view class="information_1">
									<view class="flex align-center">
										<image v-if="item.customer_id == 1" class="store_img"
											:src='STATIC_URL+"19.png"' mode="aspectFill"></image>
										<view class="information_1_1">{{item.store_name}}</view>
										<image :src='STATIC_URL+"20.png"' mode="aspectFill"></image>
									</view>
									<view class="information_1_2 flex align-center justify-center">5.0</view>
								</view>
								<view class="service_text">提供上门送车服务(可选到店自取)</view>
								<!-- <view class="information_2">
									<image :src='STATIC_URL+"225.png"' mode=""></image>
									<view class="information_2_1">取/还</view>
									<view class="information_2_2">
										<text v-if="door_to_door == 0">到店取送车，</text>
										<text v-if="door_to_door == 1">送取车上门，</text>
										<text>距离门店{{v.distance_in_meters}}</text>
									</view>
								</view> -->
							</view>
							<!-- <view class="quotation_address" @tap.stop="openLocation(v.lat, v.lng, v.store_name)">
								<image :src='STATIC_URL+"224.png"' mode="aspectFill"></image>
								<view class="address_text">地图导航</view>
							</view> -->
						</view>
						<view class="quotation_label">
							<view class="label_le">
								<template v-if="item.tags">
									<template v-for="(v1, k1) in (item.tags).split(',')">
										<!-- #ifdef MP-ALIPAY -->
										<view v-if="v == 1" class="flex align-center justify-center label_3">
											<image :src='STATIC_URL+"9.png"'></image>
											<text>芝麻免押</text>
										</view>
										<!-- #endif -->
										<!-- #ifndef MP-ALIPAY -->
										<image v-if="v1 == 1" class="label_1" :src='STATIC_URL+"219.png"'
											mode="aspectFill"></image>
										<!-- #endif -->
										<view class="label_2" v-if="v1 != 1">{{getTag(v1)}}</view>
									</template>
								</template>
							</view>
						</view>
						<view class="quotation_btm">
							<view class="distance_info flex align-center"
								@tap.stop="openLocation(item.lat, item.lng, item.store_name, item.address)">
								<text>距离您:{{item.distance_in_meters}}</text>
								<image :src='STATIC_URL+"18.png"' mode="aspectFill"></image>
							</view>
							<template v-if="is_sequence != 1">
								<text v-if="item.new_coupon_type != 0">
									<text v-if="item.new_coupon_type == 1">
										<text class="deduction" v-if="item.coupon_type == 1">
											<text class="car_price_4">团购可用</text>
										</text>
										<text class="deduction" v-if="item.coupon_type == 2 && item.coupon_price > 0">
											<text class="car_price_4">团购可抵</text>
											<text class="deduction_text">{{Number(item.coupon_price)}}</text>
										</text>
									</text>
									<text v-if="item.new_coupon_type == 2">
										<text class="deduction" v-if="item.coupon_price > 0">
											<text class="car_price_4">优惠券可抵</text>
											<text class="deduction_text">{{Number(item.coupon_price)}}</text>
										</text>
									</text>
								</text>
							</template>
							<template v-else>
								<text class="car_price_card flex align-center" v-if="item.coupon_price > 0">
									<text class="car_price_card_1">次卡已减</text>
									<text class="car_price_card_2">￥{{Number(item.coupon_price)}}</text>
								</text>
							</template>
							<view style="text-align: left;margin-left: 20rpx;">
								<view class="quotation_price">
									<text class="quotation_price_1">￥</text>
									<text class="quotation_price_2">{{Number(item.price)}}</text>
									<text class="quotation_price_3">/天</text>
								</view>
								<view class="quotation_total_price">总价：￥{{Number(item.new_price) * timeData.dayNum}}
								</view>
							</view>
							<view class="btm_3" v-if="item.rental_status == 1">已租满</view>
							<view class="btm_2" v-else>订</view>
						</view>
						<view class="rentable flex align-center justify-between" v-if="item.rental_status == 1">
							<view class="date">可租时间:{{item.expectTime.toDay_month}} {{item.expectTime.toDay_time}} - {{item.expectTime.twoDay_month}} {{item.expectTime.twoDay_time}}</view>
							<view class="operate flex align-center justify-center">前往></view>
							<view class="rentable_icon"></view>
						</view>
					</view>
				</view>
			</view>
		</template>

	</view>
</template>

<script>
	import utils from "@/common/utils/utils.js"
	export default {
		data() {
			return {
				isShow: true,
				car_id: "",
				cityInfo: "",
				timeData: "",
				selectCarInfo: "", // 选择的车辆信息
				coupon_id: "", // 团购/次卡id
				coupon_type: "",
				is_sequence: 0,
				tags: uni.getStorageSync('tag'),
				offerList: [],
				windowHeight: 1400,
				dom_height: 1000,
				door_to_door: "0", // 0到店取车  1送车上门
			}
		},
		onLoad(option) {
			let {
				car_id,
				timeData,
				is_sequence,
				coupon_id,
				coupon_type
			} = option;
			if (car_id !== undefined) this.car_id = car_id;
			if (is_sequence !== undefined) this.is_sequence = is_sequence;
			if (coupon_id !== undefined) this.coupon_id = coupon_id;
			if (coupon_type !== undefined) this.coupon_type = coupon_type;
			this.cityInfo = uni.getStorageSync('city');
			// console.log(this.cityInfo);
			if (timeData) {
				this.timeData = JSON.parse(timeData);
			} else {
				this.timeData = global.timeData;
			}
			this.selectCarInfo = JSON.parse(uni.getStorageSync('selectCar'));
			// console.log(this.selectCarInfo);
			this.getList();
		},

		methods: {
			getList() {
				this.$api.post(global.apiUrls.getOffer, {
					car_id: this.car_id,
					lng: this.cityInfo.lng,
					lat: this.cityInfo.lat,
					city_id: this.cityInfo.citycode,
					start_time: this.timeData.toDay,
					end_time: this.timeData.twoDay,
					goods_order_id: this.coupon_id,
					coupon_type: this.coupon_type
				}).then(res => {
					// console.log(res);
					this.isShow = false;
					if (res.data.code == 1) {
						this.offerList = res.data.data;
						if (this.offerList.length > 0) {
							this.offerList.forEach((item, index) => {
								if (item.rental_status == 1) {
									this.$api.post(global.apiUrls.availableRentalTime, {
										start_time: this.timeData.toDay,
										end_time: this.timeData.twoDay,
										car_id: this.car_id,
										store_id: item.store_id,
										city_id: this.cityInfo.citycode
									}).then(result => {
										// console.log(result);
										if (result.data.code == 1) {
											this.$set(this.offerList, index, { ...item, customize_available_date: result.data.data.available_from, expectTime: this.resultTime(result.data.data.available_from) });
										}
									})
								} else {
									this.$set(this.offerList, index, { ...item, customize_available_date: "", expectTime: "" });
								}
							})
						}
						console.log(this.offerList);
					} else {
						this.$message.info(res.data.info);
					}
				}).catch(err => {

				})
			},
			// 格式化时间
			resultTime(customize_available_date) {
				let timeStamp = new Date(customize_available_date).getTime();
				let toDay = customize_available_date;
				let week = utils.getWeekday(toDay);
				let twoDay = utils.getTwoDaysAfter(timeStamp, global.timeData.dayNum);
				let twoWeek = utils.getWeekday(twoDay);
				// this.timeStamp = timeStamp;
				let changeTimeData = {
					dayNum: global.timeData.dayNum,
					is_today: toDay == utils.getNowTime() ? 1 : 0,
					toDay: toDay,
					toDay_month: (toDay.substring(5, 10)).replace('-', '月') + '日',
					toDay_time: toDay.substring(11, 16),
					week: week,
					twoDay: twoDay,
					twoDay_month: (twoDay.substring(5, 10)).replace('-', '月') + '日',
					twoDay_time: twoDay.substring(11, 16),
					twoWeek: twoWeek
				}
				return changeTimeData;
			},
			goReserve(item) {
				if (global.token) {
					let storeInfo = {
						store_id: item.id,
						store_name: item.store_name,
						citycode: item.city_id,
						lng: item.lng,
						lat: item.lat,
						address: item.address
					}
					uni.setStorageSync('store', storeInfo);
					if (item.rental_status == 1) {
						uni.navigateTo({
							url: '/pages/index/reserveOrder/reserveOrder?store_id=' + item.id + '&car_id=' + this.
							car_id + '&door_to_door=' + this.door_to_door + '&is_sequence=' + this.is_sequence +
							'&coupon_id=' + this.coupon_id + '&timeData=' + JSON.stringify(item.expectTime)
						})
					} else {
						uni.navigateTo({
							url: '/pages/index/reserveOrder/reserveOrder?store_id=' + item.id + '&car_id=' + this.
							car_id + '&door_to_door=' + this.door_to_door + '&is_sequence=' + this.is_sequence +
							'&coupon_id=' + this.coupon_id
						})
					}
					
				} else {
					uni.navigateTo({
						url: '/pages/tabBar/login/login'
					})
				}
			},
			getTag(id) {
				let item = this.tags.find((item) => item.id == id);
				return item.name;
			},
			// 打开地图
			openLocation(lat, lng, store_name, address) {
				uni.openLocation({
					latitude: lat,
					longitude: lng,
					name: store_name,
					address: address,
					success: function() {
						console.log('success');
					},
					fail: function() {

					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.img_view {
		width: 100%;
		height: 440rpx;
		position: relative;

		image {
			width: 100%;
			height: 100%;
		}

		.car_tip {
			width: 242rpx;
			height: 34rpx;
			background: rgba(0, 0, 0, 0.5);
			border-radius: 18rpx;
			font-weight: 400;
			font-size: 20rpx;
			color: #FFFFFF;
			position: absolute;
			right: 14rpx;
			bottom: 37rpx;
		}
	}

	.select_vehicle {
		background: #fff;
		padding: 37rpx 35rpx 30rpx;
		border-radius: 24rpx;
		transform: translateY(-26rpx);

		.car_name {

			.car_name_1 {
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 30rpx;
				color: #111111;
				text-overflow: ellipsis;
				overflow: hidden;
				white-space: nowrap;
			}

			.car_name_2 {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 22rpx;
				color: #666666;
				margin-left: 14rpx;

				.car_name_2_1 {
					color: #CCCCCC;
				}
			}

			.car_icon {
				width: 16rpx;
				height: 25rpx;
				margin-left: 8rpx;
			}
		}

		.vehicle_label {
			display: flex;
			align-items: center;
			margin-top: 24rpx;

			.label_le {
				flex: 1;
				display: flex;
				align-items: center;

				.label_1 {
					width: 108rpx;
					height: 30rpx;
				}

				.label_2 {
					padding: 2rpx 6rpx;
					background: rgba(26, 102, 250, 0.05);
					border-radius: 4rpx;
					font-weight: 500;
					font-size: 22rpx;
					color: #506898;
					margin-left: 12rpx;
				}

				.label_3 {
					padding: 2rpx 8rpx;
					background: #1677FF;
					color: #fff;
					font-size: 22rpx;
					border-radius: 5rpx;

					image {
						width: 22rpx;
						height: 22rpx;
						margin-right: 10rpx;
					}
				}
			}

			.label_ri {
				font-size: 22rpx;
				color: #999999;
			}

		}

	}

	.quotation_list {
		transform: translateY(-16rpx);
		padding: 0 0 24rpx;

		.quotation_bg {
			background-image: url(https://speed.zhongchuxing.com/dyimages/17.png) !important;
			background-position: center;
			background-repeat: no-repeat;
			background-size: 100% 100%;
		}

		.quotation_item {
			margin-bottom: 12rpx;
			background-color: #FFFFFF;
			border-radius: 24rpx;
			padding: 32rpx 36rpx;

			.quotation_information {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.information {
					width: 100%;

					.information_1 {
						display: flex;
						align-items: center;
						justify-content: space-between;

						image {
							width: 24rpx;
							height: 24rpx;
							margin-left: 8rpx;
						}

						.information_1_1 {
							font-weight: 500;
							font-size: 32rpx;
							color: #000000;
						}

						.information_1_2 {
							font-family: DingTalk JinBuTi, DingTalk JinBuTi;
							font-weight: 600;
							font-size: 26rpx;
							color: #FC5B03;
							width: 63rpx;
							height: 36rpx;
							background: rgba(252, 91, 3, 0.1);
							border-radius: 16rpx;
						}

						.store_img {
							width: 173rpx;
							height: 36rpx;
							margin-right: 15rpx;
						}
					}

					.service_text {
						font-weight: 400;
						font-size: 24rpx;
						color: #656565;
						margin-top: 24rpx;
					}

					.information_2 {
						display: flex;
						align-items: center;
						margin-top: 16rpx;

						image {
							width: 18rpx;
							height: 12rpx;
						}

						.information_2_1 {
							font-size: 22rpx;
							color: #999999;
							margin: 0 16rpx 0 4rpx;
						}

						.information_2_2 {
							font-size: 22rpx;
							color: #111111;
						}
					}
				}

				.quotation_address {
					text-align: center;

					image {
						width: 32rpx;
						height: 32rpx;
						margin-bottom: 8rpx;
					}

					.address_text {
						font-weight: 400;
						font-size: 20rpx;
						color: #1A66FA;
					}
				}

			}

			.quotation_label {
				display: flex;
				align-items: center;
				margin-top: 16rpx;

				.label_le {
					flex: 1;
					display: flex;
					align-items: center;

					.label_1 {
						width: 108rpx;
						height: 30rpx;
					}

					.label_2 {
						padding: 2rpx 6rpx;
						background: rgba(26, 102, 250, 0.05);
						border-radius: 4rpx;
						font-weight: 500;
						font-size: 22rpx;
						color: #506898;
						margin-left: 12rpx;
					}

					.label_3 {
						padding: 2rpx 8rpx;
						background: #1677FF;
						color: #fff;
						font-size: 22rpx;
						border-radius: 5rpx;

						image {
							width: 22rpx;
							height: 22rpx;
							margin-right: 10rpx;
						}
					}
				}

				.label_ri {
					font-size: 22rpx;
					color: #999999;
				}

			}

			.quotation_btm {
				display: flex;
				align-items: flex-end;
				justify-content: flex-end;
				margin-top: 36rpx;

				.distance_info {
					margin-right: auto;
					font-weight: 400;
					font-size: 24rpx;
					color: #666666;

					image {
						width: 38rpx;
						height: 38rpx;
						margin-left: 4rpx;
					}
				}

				.quotation_total_price {
					font-weight: 500;
					font-size: 22rpx;
					color: #999999;
					margin-top: 10rpx;
				}

				.quotation_price {
					display: flex;
					align-items: flex-end;

					.quotation_price_1 {
						font-weight: 500;
						font-size: 26rpx;
						color: #666666;
						line-height: 32rpx;
					}

					.quotation_price_2 {
						font-weight: bold;
						font-size: 44rpx;
						color: #EF2E28;
						line-height: 38rpx;
					}

					.quotation_price_3 {
						font-weight: 500;
						font-size: 26rpx;
						color: #666666;
						line-height: 32rpx;
					}
				}

				.deduction {
					border-radius: 4rpx;
					border: 1px solid #F95151;

					.car_price_4 {
						font-weight: 400;
						font-size: 24rpx;
						color: #F95151;
						padding: 0 5rpx;
					}

					.deduction_text {
						display: inline-block;
						height: 100%;
						font-weight: 400;
						font-size: 24rpx;
						color: #F95151;
						padding: 0 6rpx;
						background: #FFF0F0;
						border-radius: 0px 4rpx 4rpx 0px;
						border-top: 1px solid #FFF0F0;
					}
				}

				.car_price_card {
					margin-left: 12rpx;

					.car_price_card_1 {
						font-size: 22rpx;
						color: #EF2E28;
						border: 1px solid #EF2E28;
						border-radius: 4rpx 0 0 4rpx;
						padding: 0 5rpx;
					}

					.car_price_card_2 {
						height: 100%;
						padding: 0 5rpx;
						font-size: 22rpx;
						background: #EF2E28;
						color: #fff;
						border: 1px solid #EF2E28;
						border-radius: 0 4rpx 4rpx 0;
					}
				}

				.btm_2 {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 72rpx;
					height: 72rpx;
					font-weight: 500;
					font-size: 32rpx;
					color: #FFFFFF;
					margin-left: 16rpx;
					border-radius: 10rpx;
					background: #FC5B03;
				}

				.btm_3 {
					display: flex;
					align-items: center;
					justify-content: center;
					width: 110rpx;
					height: 72rpx;
					font-weight: 500;
					font-size: 30rpx;
					color: #FFFFFF;
					margin-left: 16rpx;
					border-radius: 10rpx;
					background: #FECEB3;
				}

				.quotation_price_4 {
					padding: 2rpx 6rpx;
					background: rgba(252, 91, 3, 0.07);
					border-radius: 4rpx;
					font-weight: 500;
					font-size: 22rpx;
					color: #FC5B03;
					margin-right: 12rpx;
				}
			}

			.rentable {
				position: relative;
				background: #FFF7F2;
				border: 1px solid #FC5B03;
				border-radius: 18rpx;
				padding: 9rpx 10rpx;
				margin-top: 30rpx;
				z-index: 1;

				.date {
					font-weight: 400;
					font-size: 28rpx;
					color: #FC5B03;
				}

				.operate {
					width: 89rpx;
					height: 46rpx;
					background: #FC5B03;
					border-radius: 10rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;
					position: relative;
					z-index: 3;
				}

				.rentable_icon {
					// background: rgba(252,91,3,0.05);
					background: #FFF7F2;
					width: 22rpx;
					height: 22rpx;
					border-left: 1px solid #FC5B03;
					border-top: 1px solid #FC5B03;
					position: absolute;
					right: 44rpx;
					top: -14rpx;
					transform: rotate(45deg);
					z-index: 2;
				}
			}
		}
	}
</style>